package com.haidechizi.designpattern.proxy.dynamic.jdk;

import com.google.common.base.Stopwatch;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

/**
 * @author haidechizi
 */
public class DynamicLog implements InvocationHandler {

    private Object obj;

    public DynamicLog(Object obj) {
        this.obj = obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("开始调用方法");
        Stopwatch stopwatch = Stopwatch.createStarted();
        Object result = method.invoke(obj, args);
        System.out.println("方法调用结束，调用时长: " + stopwatch.elapsed(TimeUnit.MILLISECONDS));
        return result;
    }
}
